package cn.piesat.util.config;

import cn.piesat.service.intf.ResourceService;
import cn.piesat.service.intf.RoleService;
import cn.piesat.util.JedisUtils;
import com.alibaba.fastjson.JSON;
import cn.piesat.vo.role.RoleUrlProjectionVO;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zyp
 * @date 2021/3/11
 */
@Component
public class InitResourceRolesCacheRunner implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(InitResourceRolesCacheRunner.class);
    @Autowired
    private RoleService roleService;
    @Autowired
    private ResourceService resourceService;

    @Override
    public void run(String... args) throws Exception {

        Jedis jedis = JedisUtils.getJedis();
        if(jedis !=null){
            try{
                logger.info("开始初始化映射至Redis角色访问路径信息");
                List<RoleUrlProjectionVO> roleUrlProjections = roleService.listRoleUrl();
                Map<String, String> cacheMap = roleUrlProjections
                        .stream()
                        .filter(a ->a.getMarkListStr() !=null)
                        .collect(Collectors.toMap(RoleUrlProjectionVO::getUrl
                                , projection -> {
                                    String[] strArr = projection.getMarkListStr().split(",");
                                    JSONObject jsonObject = new JSONObject();
                                    jsonObject.put("name",projection.getName());
                                    jsonObject.put("markList",Arrays.stream(strArr)
                                            .map(roleMark -> "ROLE_" + roleMark)
                                            .collect(Collectors.toList())
                                     );
                                    return JSON.toJSONString(jsonObject);
                                }
                                , (k1, k2) -> k2));
                jedis.del("auth:resourceRoles");
                if (!cacheMap.isEmpty()) {
                    jedis.hmset("auth:resourceRoles",cacheMap);
                }
                logger.info("Redis角色访问路径信息初始化成功");
                logger.info("开始初始化白名单映射");
                Map<String,String> whiteMap = resourceService.findByMenuId();
                jedis.del("auth:whiteList");
                if(!whiteMap.isEmpty()){
                    jedis.hmset("auth:whiteList",whiteMap);
                }
                logger.info("初始化白名单映射成功");
                logger.info("开始初始化日志记录映射");
                Map<String,String> logMap = resourceService.initLog();
                jedis.del("auth:statusList");
                if (!logMap.isEmpty()) {
                    jedis.hmset("auth:statusList",logMap);
                }
                logger.info("初始化日志记录映射完成");
            }finally {
                jedis.close();
            }
        }
    }
}
